package com.heima.wemedia;

import io.jsonwebtoken.lang.Collections;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.IOException;

public class ReplicaDatas {
    /**
     * 直接通过调用修改状态方法，将所有爬虫数据 设置通过
     */
    Document document = null;
    @BeforeEach
    public void initDriver(){
        System.setProperty("webdriver.chrome.driver", "D:\\JAVA\\Develop\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://3g.163.com/touch/ent/?ver=c&clickfrom=index2018_header_main");
        document = Jsoup.parse(driver.getPageSource());
    }

    @Test
    public void JsoupDemo() throws IOException {
    /*    document = Jsoup.connect("https://3g.163.com/touch/ent/?ver=c&clickfrom=index2018_header_main")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36")
                .get();*/

       //根据标签获得文章内容
        Elements articleList = document.getElementsByTag("article");
        for (Element element : articleList) {
            //获取标题标签
                Elements h3List = element.getElementsByTag("h3");
                //如果标签不中的内容为空那就继续循环
            if (Collections.isEmpty(h3List)) {
                continue;
            }
            //输出不为空的第一个索引
            Element h3Ele = h3List.get(0);
            //获取标签中的text的文本
            System.out.println(h3Ele.text());
        }
        System.out.println("======================");
        for (Element element : articleList) {
            //获得带有文章路径的a标签
            Elements aList = element.getElementsByTag("a");
            if (Collections.isEmpty(aList)) {
                continue;
            }
            //获取当前标签中属性的值 href
            String href = aList.get(0).attr("href");
            System.out.println("文章路径：" + href);
        }
        System.out.println("======================");
        for (Element element : articleList) {
            //获得带有文章路径的a标签
            Elements imgList = element.getElementsByTag("img");
            for (Element img : imgList) {
                System.out.println("封面图片" + img.attr("data-src"));
            }


        }
    }

}
